//
//  YYPNewFeatureController.swift
//  weibo
//
//  Created by youyongpeng on 15/11/24.
//  Copyright © 2015年 youyongpeng. All rights reserved.
//

import UIKit

/// 可重用id
private let WBYYPNewFeatureCell = "WBYYPNewFeatureCell"

// 画面个数
private let WBNewFeatureCount = 4

class YYPNewFeatureController: UICollectionViewController {

    /// MARK: - 构造函数
    // 不能使用懒加载创建 layout ,因为懒加载实在控制器实例化之后才创建的,所以不能使用
    init() {
    
        let layout = UICollectionViewFlowLayout()
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // 注册可重用 cell
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: WBYYPNewFeatureCell)

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
    }
    */

    // MARK: 数据源方法


    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return WBNewFeatureCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WBYYPNewFeatureCell, forIndexPath: indexPath) as! NewFeatureCell
        
        cell.iconIndex = indexPath.item
    
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        // 计算当前页
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
        
        // 判断一下是否是最后一页
        if page != WBNewFeatureCount - 1 {
            
            return
        }
        
        // 找到当前cell
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: page, inSection: 0)) as! NewFeatureCell
        
        // 动画
        cell.buttonAnim()
    }
}

/// MARK: - 新特性cell
private class NewFeatureCell: UICollectionViewCell {

    /// MARK: - 懒加载
    private let iconView: UIImageView = UIImageView()
    private let newButton: UIButton = UIButton(title: "开始体验", imageName: "new_feature_finish_button", color: UIColor.whiteColor())
    
    // 图片的索引
    private var iconIndex: Int = 0 {
    
        didSet{
            
            iconView.image = UIImage(named: "new_feature_\(iconIndex + 1)")
            
            newButton.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupInfo()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 添加图片
    private func setupInfo() {
    
        addSubview(iconView)
        addSubview(newButton)
        
        iconView.frame = bounds
        
        newButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
        
        newButton.addTarget(self, action: "clickStartButton", forControlEvents: .TouchUpInside)
    }
    
    // 动画
    private func buttonAnim() {
    
        newButton.hidden = false
        newButton.transform = CGAffineTransformMakeScale(0, 0)
//        newButton.userInteractionEnabled = false
        
        /// 动画
        ///
        /// - parameter <Tduration:             动画时长
        /// - parameter delay:                  延迟时间
        /// - parameter usingSpringWithDamping: 弹力系数
        /// - parameter initialSpringVelocity:  初始速度,一般是10
        /// - parameter options:                动画选项
        UIView.animateWithDuration(1.6, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: [], animations: { () -> Void in
            
            self.newButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
            
            self.newButton.userInteractionEnabled = true
        }
    }
    
    // 按钮点击事件
    @objc private func clickStartButton() {
        
        print("点我了")
    }
    
}
